// pages/user-profile-vip/user-profile-vip.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    onhidden: true,
    hidden: true,
  },

  vip1() {
    // console.log(this.data.arr.users[0].credit)
    var credit = this.data.arrcredit[0].credit
    var vvip = this.data.arr.users[0].vip
    //判断用户是否vip对象
    if (vvip == "vip1" || vvip == "vip2" || vvip == "vip3") {
      console.log("你已经是vip1尊贵客户了！")
      wx.showModal({
        title: '',
        content: '你已经是vip1尊贵客户了！',
      })
    } else {
      // 判断积分是否满足要求
      if (credit < 30) {
        console.log("你的积分不足，注册vip1失败")
        wx.showModal({
          title: '',
          content: '你的积分不足，注册vip1失败',
        })
      } else {
        var id = this.data.arr.users[0].id
        var vip = "vip1"
        credit = credit - 30
        var that = this;
        wx.request({
          url: 'http://localhost/shopping/users_credit_del.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            username: that.data.name,
            credit: credit,
          },
          success: function (res) {
            // console.log(res.data)
          },
          fail: function (res) {
            console.log("fail");
          }
        })
        wx.request({
          url: 'http://localhost/shopping/users_vip.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            id: id,
            vip: vip,
          },
          success: function (res) {
            // console.log(res.data)
            console.log("感谢您！成为了vip1尊贵客户")
            that.Vip();
            wx.showModal({
              title: '',
              content: '感谢您！成为了vip1尊贵客户!',
            })
            that.setData({})
          },
          fail: function (res) {
            console.log("fail");
          }
        })
      }
    }
  },
  vip2() {
    var credit = this.data.arrcredit[0].credit
    var vvip = this.data.arr.users[0].vip
    //判断用户是否vip对象
    if (vvip == "vip2" || vvip == "vip3") {
      console.log("你已经是vip2尊贵客户了！")
      wx.showModal({
        title: '',
        content: '你已经是vip2尊贵客户了！',
      })
    } else {
      // 判断积分是否满足要求
      if (credit < 60) {
        console.log("你的积分不足，注册vip2失败")
        wx.showModal({
          title: '',
          content: '你的积分不足，注册vip2失败',
        })
      } else {
        var id = this.data.arr.users[0].id
        var vip = "vip2"
        credit = credit - 60

        var that = this;
        wx.request({
          url: 'http://localhost/shopping/users_credit_del.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            username: that.data.name,
            credit: credit,
          },
          success: function (res) {
            // console.log(res.data)
          },
          fail: function (res) {
            console.log("fail");
          }
        })
        wx.request({
          url: 'http://localhost/shopping/users_vip.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            id: id,
            vip: vip,
          },
          success: function (res) {
            // console.log(res.data)
            if (vvip == "vip1") {
              console.log("恭喜你！您的vip等级升级vip2")
              that.Vip();
              wx.showModal({
                title: '',
                content: '恭喜你！您的vip等级升级vip2',
              })
            } else {
              console.log("感谢您！成为了vip2尊贵客户")
              that.Vip();
              wx.showModal({
                title: '',
                content: '感谢您！成为了vip2尊贵客户',
              })
            }
            that.setData({})
          },
          fail: function (res) {
            console.log("fail");
          }
        })
      }
    }
  },
  vip3() {
    var credit = this.data.arrcredit[0].credit
    var vvip = this.data.arr.users[0].vip
    //判断用户是否vip对象
    if (vvip == "vip3") {
      console.log("你已经是vip3尊贵客户了！")
      wx.showModal({
        title: '',
        content: '你已经是vip3尊贵客户了！',
      })
    } else {
      // 判断积分是否满足要求
      if (credit < 90) {
        console.log("你的积分不足，注册vip3失败")
        wx.showModal({
          title: '',
          content: '你的积分不足，注册vip3失败',
        })
      } else {
        var id = this.data.arr.users[0].id
        var vip = "vip3"
        credit = credit - 90

        var that = this;
        wx.request({
          url: 'http://localhost/shopping/users_credit_del.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            username: that.data.name,
            credit: credit,
          },
          success: function (res) {
            // console.log(res.data)
          },
          fail: function (res) {
            console.log("fail");
          }
        })
        wx.request({
          url: 'http://localhost/shopping/users_vip.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            id: id,
            vip: vip,
          },
          success: function (res) {
            // console.log(res.data)
            if (vvip == "vip1" || vvip == "vip2") {
              console.log("恭喜你！您的vip等级升级vip3")
              that.Vip();
              wx.showModal({
                title: '',
                content: '恭喜你！您的vip等级升级vip3',
              })
            } else {
              console.log("感谢您！成为了vip3尊贵客户")
              that.Vip();
              wx.showModal({
                title: '',
                content: '感谢您！成为了vip3尊贵客户',
              })
            }
            that.setData({})
          },
          fail: function (res) {
            console.log("fail");
          }
        })
      }
    }
  },
  Vip: function () {

    var that = this;
    var name = that.data.name
    wx.request({
      url: 'http://localhost/shopping/users.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        console.log(res.data)
        if (!res.data.users[0].vip) {
          console.log('您不是会员！')
          that.setData({
            onhidden: !that.data.onhidden,
          })
        } else {
          console.log('您已经是会员！')
          that.setData({
            hidden: !that.data.hidden,
          })
          var vip = res.data.users[0].vip
          var img = res.data.users[0].img
        }
        that.setData({
          arraa: res.data.users,
          arr: res.data,
          vip: vip,
          img: img
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/users_credit.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrcredit: res.data.credit,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var name = e.name
    this.setData({
      name: name
    })
    this.Vip();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})